# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: leon

import os

from absl import app, flags

import pandas
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib.ticker import FormatStrFormatter

FLAGS = flags.FLAGS


def plot_spread(df, csv_root, exchange, date):
  fig, ax1 = plt.subplots(figsize=(20, 10))

  ax2 = ax1.twinx()
  ax1.tick_params(axis='x', rotation=45)
  symbols = df['symbol']
  plt.xticks(df.index, symbols)
  ax1.plot(df.index, df['avg_bid_ask_spread/vwap'], color='g', marker='o')
  ax1.yaxis.set_major_formatter(FormatStrFormatter('%.5f'))
  ax2.plot(df.index, df['total_volume*vwap'], color='b', marker='o')

  ax1.set_ylabel('bid_ask_spread', color='g')
  ax2.set_ylabel('volume USDT', color='b')
  plt.savefig(os.path.join(csv_root, '%s_30_top_volume_spread_%d.png' % (exchange, date)))


def read_csv_into_df(csv_root, csv):
  csv_dir = os.path.join(csv_root, csv)
  df = pandas.read_csv(csv_dir, sep=',', header=0)
  return df


def main(argv):
  csv_root = FLAGS.csv_root
  exchange = FLAGS.exchange
  assert csv_root, '--csv_root must be specified.'
  assert exchange, '--exchange must be specified.'

  for date in range(20180619, 20180626, 1):
    spread_df = read_csv_into_df(csv_root, '%s_20_top_volume_%d.csv' % (exchange, date))
    plot_spread(spread_df, csv_root, exchange, date)

  return 0


if __name__ == '__main__':
  flags.DEFINE_string('csv_root', None, 'Input csv files root directory.')

  flags.DEFINE_string('exchange', None, 'Exchange name.')

  app.run(main)
